import 'package:boss/models/package_group.dart';
import 'package:boss/provider/package_group_provider.dart';
import 'package:boss/ui/page/packageGroup/page/packageGroupDetail/provider/package_group_detail_provider.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'widget/package_group_detail_browser.dart';

@RouterConfig(PackageGroupDetailPage.pageName)
class PackageGroupDetailPage extends StatefulWidget {
  static const pageName = '/packageGroupDetailPage';

  @Autowired()
  final PackageGroup packageGroup;

  const PackageGroupDetailPage({Key key, this.packageGroup}) : super(key: key);

  @override
  _PackageGroupDetailPageState createState() => _PackageGroupDetailPageState('套餐分组详情');
}

class _PackageGroupDetailPageState extends BaseState<PackageGroupDetailPage> {
  _PackageGroupDetailPageState(String pageName) : super(pageName);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (ctx) => PackageGroupDetailProvider(
        packageGroup: widget.packageGroup ?? PackageGroup.fromJson({}),
      ),
      child: Scaffold(
        appBar: CenterTitleAppBar(
          title: '套餐分组详情',
          actions: <Widget>[
            if (widget.packageGroup?.id != null)
              LabelButton(
                title: '删除',
                padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                onTap: () async {
                  var provider = Provider.of<PackageGroupProvider>(
                    context,
                    listen: false,
                  );
                  if (await provider.removePackageGroup(widget.packageGroup.id)) {
                    Navigator.pop(context);
                  }
                },
              ),
          ],
        ),
        body: PackageGroupDetailBrowser(),
      ),
    );
  }
}
